package edu.buffalo.cse.cse486_586.simpledynamo.provider;

import edu.buffalo.cse.cse486_586.simpledynamo.util.AppUtil;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class KeyValueDbHelper extends SQLiteOpenHelper {
	// Debugging
	private static final String TAG = "KeyValueDbHelper";
	//private static final boolean D = true;

	// declare constants fields
	private static final int DB_VERSION = 1;

	// declared constant SQL Expression
	private static final String DB_CREATE = "CREATE TABLE " + AppUtil.KeyValueTable.KV_TABLE + " ( "
			+ AppUtil.KeyValueTable.COL_ID + " integer PRIMARY KEY AUTOINCREMENT, "
			+ AppUtil.KeyValueTable.COL_PROVIDER_KEY + " text NOT NULL, "
			+ AppUtil.KeyValueTable.COL_PROVIDER_VALUE + " text NOT NULL, "
			+ AppUtil.KeyValueTable.COL_PROVIDER_VERSION + " text NOT NULL" +" );";

	private static final String DB_DESTROY = "DROP TABLE IF EXISTS " + AppUtil.KeyValueTable.KV_TABLE;

	/*
	 * constructor
	 */
	public KeyValueDbHelper(Context context) {
		super(context, AppUtil.DATABASE_NAME, null, DB_VERSION);
		Log.d(TAG, "constructor");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
	 * .SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d(TAG, "helper on create");
		db.execSQL(DB_CREATE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(DB_DESTROY);
		onCreate(db);
	}
}
